package com.chance.cc.crawler.prod.command.job.domain.vm.bitauto.adhoc;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.bitauto.BitAutoCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.Tag_Site_Info;

/**
 * @Author: ZhaoHhuan on 2021/11/1 15:37
 * @Email: 18638575967@163.com
 * @Description:
 *      易车车家号全部文章采集
**/
public class BitAutoWebHaoArticleAllSimpleCrawlerSchedulerJob extends BitAutoCommonCrawlerSchedulerJob {
    private static final String crawler_level = "simple";

    public static void main(String[] args) {
        publishBitAutoCrawlerSchedulerJobInfo();
    }
    public static CrawlerJob publishBitAutoCrawlerSchedulerJobInfo(){
        CrawlerJob bitAutoScheduler = bitAutoScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(bitAutoScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("易车号新闻全部文章临时采集");
        crawlerScheduleJob.setCrawlerKey(bitAutoScheduler.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return bitAutoScheduler;
    }

    public static CrawlerJob bitAutoScheduler(){
        CrawlerRequestRecord haoArticleCrawlerComment = doHaoArticleCommentTraceCrawler();
        String site = haoArticleCrawlerComment.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("site");
        //request队列名
        String requestQueueName = StringUtils.joinWith("-","crawler",domain, site,"all", crawler_level,"queue");

        return CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,"all",crawler_level,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .kafkaResultPipeline("kafka_result",kafkaTopciForRealTimeJob,null)
                .fileResultPipeline("kafka_result","/data/chance_crawler_runner/logs/node/bitauto/hao_article_all.log",false)
                .crawlerJobThreadNumber(40)
                .requestRecord(haoArticleCrawlerComment)
                .build();
    }

    public static CrawlerRequestRecord doHaoArticleCommentTraceCrawler(){
        String url = "https://news.yiche.com/info/categoryId0_p0_l0_f0_g0_c0_b0_1.html";
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(domain)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.memoryFilterKeyInfo(domain))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(0,new long[]{1637251200000L,System.currentTimeMillis()}))
                .releaseTime(System.currentTimeMillis())
                .build();
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite("hao_article");
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz("hao_article");//脚本中固定死的
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,"hao_article_all");

        return crawlerRequestRecord;
    }
}
